﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SGP;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace AnimationTool
{
    public partial class Form1 : Form
    {
        bool looping = true;
        CSGP_Direct3D D3D = CSGP_Direct3D.GetInstance();
        CSGP_TextureManager TM = CSGP_TextureManager.GetInstance();
        List<CAnimation> animList = new List<CAnimation>();
        List<string> imageList = new List<string>();
        List<int> imageIndexs = new List<int>();
        int currSpriteIndex = -1;

        Point origin = new Point(0, 0); 
        Point offset = Point.Empty;

        //bool preview = false;
        //bool loopingPre = true;
        bool drawRects = false;
        bool pause = false;
        bool playOnce = false;
        int currFrame = 0;
        float timeWaited = 0.0f;
        

       public void Update(float changTime)
        {
            pause = !loopCheckBox.Checked;
            if (pause == false && animationListBox.SelectedIndex != -1 || playOnce == true && currFrame > -1)
            {
                timeWaited += changTime;

                // Is it time to move to the next frame?
                if (currFrame < animList[animationListBox.SelectedIndex].frameList.Count && currFrame != -1)
                {
                    if (timeWaited >= animList[animationListBox.SelectedIndex].frameList[currFrame].FDuration)
                    {
                        timeWaited = 0.0f;
                        ++currFrame;

                        // Has it reached the end?
                        if (currFrame == animList[animationListBox.SelectedIndex].frameList.Count)
                        {
                            // Should the animation loop from the beginning?
                            currFrame = 0;
                            playOnce = false;
                        }

                    }
                }
            }
        }

       public void Render()
        {
            
            
            D3D.Clear(panel2, Color.Indigo);
            D3D.Clear(panel1, Color.ForestGreen);
             { 
                 D3D.DeviceBegin();
                 D3D.SpriteBegin();

                 offset.X = origin.X + panel1.AutoScrollPosition.X;
                 offset.Y = origin.Y + panel1.AutoScrollPosition.Y; 
                 
                 
                 // render Here
                 if (currSpriteIndex != -1)
                 {
                     
                     TM.Draw(currSpriteIndex, offset.X, offset.Y);

                     if (animationListBox.SelectedIndex != -1)
                     {

                         for (int i = 0; i < animList[animationListBox.SelectedIndex].frameList.Count; i++)
                         {
                             int font = 0;
                             Color color;
                             if (frameListBox.SelectedIndex == i)
                             {
                                 color = Color.Red;
                                 font = 3;
                             }
                             else
                             {
                                 color = Color.Tomato;
                                 font = 2;
                             }

                             Point point = new Point(animList[animationListBox.SelectedIndex].frameList[i].sourceRect.hitBoxX + offset.X, animList[animationListBox.SelectedIndex].frameList[i].sourceRect.hitBoxY + offset.Y);
                             Size size = new Size(animList[animationListBox.SelectedIndex].frameList[i].sourceRect.hitBoxWidth, animList[animationListBox.SelectedIndex].frameList[i].sourceRect.hitBoxLength);
                             Rectangle rect = new Rectangle(point, size);

                             D3D.DrawHollowRect(rect, color, font);

                             Color color2 = Color.Yellow;
                             Point point2 = new Point(animList[animationListBox.SelectedIndex].frameList[i].FanchorX - 5 + offset.X, animList[animationListBox.SelectedIndex].frameList[i].FanchorY - 5 + offset.Y);
                             Size size2 = new Size(10, 10);
                             Rectangle rect2 = new Rectangle(point2, size2);
                             D3D.DrawRect(rect2, color2);

                             

                             for (int c = 0; c < animList[animationListBox.SelectedIndex].frameList[i].activeHitBoxList.Count; c++)
                             {
                                 if (activeListBox.SelectedIndex == c && frameListBox.SelectedIndex == i)
                                 {
                                     color = Color.DarkBlue;
                                     font = 2;
                                 }
                                 else
                                 {
                                     color = Color.Blue;
                                     font = 1;
                                 }
                                 Point point3 = new Point(animList[animationListBox.SelectedIndex].frameList[i].activeHitBoxList[c].hitBoxX + offset.X, animList[animationListBox.SelectedIndex].frameList[i].activeHitBoxList[c].hitBoxY + offset.Y);
                                 Size size3 = new Size(animList[animationListBox.SelectedIndex].frameList[i].activeHitBoxList[c].hitBoxWidth, animList[animationListBox.SelectedIndex].frameList[i].activeHitBoxList[c].hitBoxLength);
                                 Rectangle rect3 = new Rectangle(point3, size3);
                                 D3D.DrawHollowRect(rect3, color, font);
                             }

                             if ( frameListBox.SelectedIndex == i)
                             {
                                 color = Color.DarkGreen;
                                 font = 2;
                             }
                             else
                             {
                                 color = Color.Green;
                                 font = 1;
                             }

                             Point point4 = new Point(animList[animationListBox.SelectedIndex].frameList[i].passiveHitBox.hitBoxX + offset.X, animList[animationListBox.SelectedIndex].frameList[i].passiveHitBox.hitBoxY + offset.Y);
                             Size size4 = new Size(animList[animationListBox.SelectedIndex].frameList[i].passiveHitBox.hitBoxWidth, animList[animationListBox.SelectedIndex].frameList[i].passiveHitBox.hitBoxLength);
                             Rectangle rect4 = new Rectangle(point4, size4);

                             D3D.DrawHollowRect(rect4, color, font);
                         } 
                     }
                 }

                 D3D.SpriteEnd();
                 D3D.DeviceEnd();
                 D3D.Present();
             }
             if (currSpriteIndex != -1 && animationListBox.SelectedIndex > -1)
                {
                    PreRender();
                }
             
             
        }

        public void PreRender()
       {
            D3D.Clear(panel2, Color.ForestGreen);
             { 
                 D3D.DeviceBegin();
                 D3D.SpriteBegin();

                 
                 
                     if (animList[animationListBox.SelectedIndex].frameList.Count > 0 && currFrame < animList[animationListBox.SelectedIndex].frameList.Count && currFrame != -1)
                     {
                         Point point5 = new Point(animList[animationListBox.SelectedIndex].frameList[currFrame].sourceRect.hitBoxX, animList[animationListBox.SelectedIndex].frameList[currFrame].sourceRect.hitBoxY);
                         Size size5 = new Size(animList[animationListBox.SelectedIndex].frameList[currFrame].sourceRect.hitBoxWidth, animList[animationListBox.SelectedIndex].frameList[currFrame].sourceRect.hitBoxLength);
                         Rectangle rect5 = new Rectangle(point5, size5);
                         TM.Draw(currSpriteIndex, 0, 0, 1.0f, 1.0f, rect5);

                         drawRects = rectCheckBox.Checked;
                         if (drawRects == true)
                         {
                             Color color6 = Color.Yellow;
                             Point point6 = new Point(animList[animationListBox.SelectedIndex].frameList[currFrame].FanchorX - 5 + offset.X, animList[animationListBox.SelectedIndex].frameList[currFrame].FanchorY - 5 + offset.Y);
                             Size size6 = new Size(10, 10);
                             Rectangle rect6 = new Rectangle(point6, size6);
                             D3D.DrawRect(rect6, color6);

                             Color color7;
                             int font7;


                             color7 = Color.Blue;
                             font7 = 1;

                             for (int c = 0; c < animList[animationListBox.SelectedIndex].frameList[currFrame].activeHitBoxList.Count; c++)
                             {
                                 Point point7 = new Point(animList[animationListBox.SelectedIndex].frameList[currFrame].activeHitBoxList[c].hitBoxX + offset.X, animList[animationListBox.SelectedIndex].frameList[currFrame].activeHitBoxList[c].hitBoxY + offset.Y);
                                 Size size7 = new Size(animList[animationListBox.SelectedIndex].frameList[currFrame].activeHitBoxList[c].hitBoxWidth + offset.X, animList[animationListBox.SelectedIndex].frameList[currFrame].activeHitBoxList[c].hitBoxLength + offset.Y);
                                 Rectangle rect7 = new Rectangle(point7, size7);
                                 D3D.DrawHollowRect(rect7, color7, font7);
                             }

                             color7 = Color.Green;
                             font7 = 1;

                             Point point8 = new Point(animList[animationListBox.SelectedIndex].frameList[currFrame].passiveHitBox.hitBoxX + offset.X, animList[animationListBox.SelectedIndex].frameList[currFrame].passiveHitBox.hitBoxY + offset.Y);
                             Size size8 = new Size(animList[animationListBox.SelectedIndex].frameList[currFrame].passiveHitBox.hitBoxWidth + offset.X, animList[animationListBox.SelectedIndex].frameList[currFrame].passiveHitBox.hitBoxLength + offset.Y);
                             Rectangle rect8 = new Rectangle(point8, size8);

                             D3D.DrawHollowRect(rect8, color7, font7);
                         }
                     }
             }

             D3D.SpriteEnd();
             D3D.DeviceEnd();
             D3D.Present();
       }

        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public Form1()
        {
            InitializeComponent();

            /*
             *  D3D.Init (panel, false)
             *  d3d.AddRenderTarget(otherpanel)
             * */

            D3D.Initialize(panel1, false);
            D3D.Initialize(panel2, false);
            D3D.AddRenderTarget(panel2);
            panel1.AutoScroll = true;
            panel1.HorizontalScroll.Enabled = true;
            panel1.VerticalScroll.Enabled = true;



            TM.Initialize(D3D.Device, D3D.Sprite);
        }

        public void Initialize()
        {
            
        }

        private void openFileMenuButton_Click(object sender, EventArgs e)
        {
            
            //animationListBox.SelectedIndex = 0;

            OpenFileDialog dlg = new OpenFileDialog();

            if (Directory.Exists("\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites"))
                Directory.SetCurrentDirectory("\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites");

            //dlg.InitialDirectory = Directory.GetCurrentDirectory();
            dlg.Filter = "XML Files(*.xml*)|*.xml";

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\Sprites"))
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\Sprites");

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Sprites"))
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Sprites");

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Sprites"))
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Sprites");

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites"))
            {
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites");
                dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites");
            }

            dlg.InitialDirectory = Directory.GetCurrentDirectory();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                animList.Clear();
                animationListBox.Items.Clear();
                

                XmlTextReader reader = new XmlTextReader(dlg.FileName);
                while (reader.Read())
                {


                    reader.ReadToFollowing("animations");
                    if (reader.EOF == true)
                        break;
                    reader.MoveToFirstAttribute();
                    int numAnim = Int32.Parse(reader.Value);

                    for (int x = 0; x < numAnim; x++)
                    {
                        CAnimation tempAnim = new CAnimation();
                        reader.ReadToFollowing("animName");
                        string animName = reader.ReadElementContentAsString();

                        reader.ReadToFollowing("animImage");
                        string animImage = reader.ReadElementContentAsString();


                        if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\Images"))
                            Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\Images");
                        imageList.Add(animImage);

                        if (animImage != "")
                        {
                            currSpriteIndex = TM.LoadTexture(animImage);// can add scale
                            imageIndexs.Add(currSpriteIndex);
                            Bitmap tempbmp = new Bitmap(animImage);
                            panel1.AutoScrollMinSize = tempbmp.Size;
                            anchorXNumericUpDown.Maximum = tempbmp.Size.Width;
                            anchorYNumericUpDown.Maximum = tempbmp.Size.Height;
                            sourceXNumericUpDown.Maximum = tempbmp.Size.Width;
                            sourceYNumericUpDown.Maximum = tempbmp.Size.Height;
                            sourceWNumericUpDown.Maximum = tempbmp.Size.Width;
                            sourceHNumericUpDown.Maximum = tempbmp.Size.Height;
                            activeXNumericUpDown.Maximum = tempbmp.Size.Width;
                            activeYNumericUpDown.Maximum = tempbmp.Size.Height;
                            activeWNumericUpDown.Maximum = tempbmp.Size.Width;
                            activeHNumericUpDown.Maximum = tempbmp.Size.Height;
                            passiveXNumericUpDown.Maximum = tempbmp.Size.Width;
                            passiveYNumericUpDown.Maximum = tempbmp.Size.Height;
                            passiveWNumericUpDown.Maximum = tempbmp.Size.Width;
                            passiveHNumericUpDown.Maximum = tempbmp.Size.Height;
                        }

                        reader.ReadToFollowing("testInfo");
                        reader.MoveToFirstAttribute();
                        int numFrames = Int32.Parse(reader.Value);

                        reader.MoveToNextAttribute();
                        int backColorR = Int32.Parse(reader.Value);

                        reader.MoveToNextAttribute();
                        int backColorG = Int32.Parse(reader.Value);

                        reader.MoveToNextAttribute();
                        int backColorB = Int32.Parse(reader.Value);

                        reader.MoveToNextAttribute();
                        int isP = Int32.Parse(reader.Value);
                        bool isPlaying = true;
                        if (isP == 1)
                        {
                            isPlaying = true;
                        }
                        else
                            isPlaying = false;

                        reader.MoveToNextAttribute();
                        int isL = Int32.Parse(reader.Value);
                        bool isLooping = true;
                        if (isL == 1)
                        {
                            isLooping = true;
                        }
                        else
                            isLooping = false;

                        tempAnim.AnimName = animName;
                        tempAnim.ImageName = animImage;
                        tempAnim.BackColorR = backColorR;
                        tempAnim.BackColorG = backColorG;
                        tempAnim.BackColorB = backColorB;
                        tempAnim.NumFrames = numFrames;
                        tempAnim.IsLooping = isLooping;
                        tempAnim.IsPlaying = isPlaying;

                        animList.Add(tempAnim);

                        for (int i = 0; i < numFrames; i++)
                        {

                            CFrame tempFrame = new CFrame();

                            reader.ReadToFollowing("frame");
                            reader.MoveToFirstAttribute();
                            float duration = float.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int sourceL = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int sourceT = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int sourceR = Int32.Parse(reader.Value) - sourceL;

                            reader.MoveToNextAttribute();
                            int sourceB = Int32.Parse(reader.Value) - sourceT;

                            reader.MoveToNextAttribute();
                            int anchorX = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int anchorY = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            string trigger = reader.Value;

                            reader.MoveToNextAttribute();
                            int numAR = Int32.Parse(reader.Value);

                            for (int ar = 0; ar < numAR; ar++)
                            {
                                CFrame.Rect tempRect = new CFrame.Rect();

                                reader.ReadToFollowing("active");
                                reader.MoveToNextAttribute();
                                int activeL = Int32.Parse(reader.Value);

                                reader.MoveToNextAttribute();
                                int activeT = Int32.Parse(reader.Value);

                                reader.MoveToNextAttribute();
                                int activeR = Int32.Parse(reader.Value);

                                reader.MoveToNextAttribute();
                                int activeB = Int32.Parse(reader.Value);

                                tempRect.HitBoxX = activeL;
                                tempRect.HitBoxY = activeT;
                                tempRect.HitBoxWidth = activeR - activeL;
                                tempRect.HitBoxLength = activeB - activeT;

                                tempRect.ActiveName = "Active Hitbox" + ar;

                                tempFrame.activeHitBoxList.Add(tempRect);
                            }

                            CFrame.Rect tempPass = new CFrame.Rect();

                            reader.ReadToFollowing("passive");
                            reader.MoveToNextAttribute();
                            int passiveL = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int passiveT = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int passiveR = Int32.Parse(reader.Value);

                            reader.MoveToNextAttribute();
                            int passiveB = Int32.Parse(reader.Value);

                            tempPass.HitBoxX = passiveL;
                            tempPass.HitBoxY = passiveT;
                            tempPass.HitBoxWidth = passiveR;
                            tempPass.HitBoxLength = passiveB;

                            tempFrame.passiveHitBox = tempPass;

                            tempFrame.FDuration = duration;
                            tempFrame.FanchorX = anchorX;
                            tempFrame.FanchorY = anchorY;
                            tempFrame.sourceRect.HitBoxX = sourceL;
                            tempFrame.sourceRect.HitBoxY = sourceT;
                            tempFrame.sourceRect.HitBoxWidth = sourceR;
                            tempFrame.sourceRect.HitBoxLength = sourceB;

                            tempFrame.Strigger = trigger;

                            tempFrame.Name1 = "frame" + i;

                            animList[x].frameList.Add(tempFrame);

                        }
                    }
                }

                for (int a = 0; a < animList.Count; a++)
                    animationListBox.Items.Add(animList[a]);

                animNameTextBox.Text = animList[0].AnimName;
                imageNameTextBox.Text = animList[0].ImageName;
                redNumericUpDown.Value = animList[0].BackColorR;
                greenNumericUpDown.Value = animList[0].BackColorG;
                blueNumericUpDown.Value = animList[0].BackColorB;
                loopingCheckBox.Checked = animList[0].IsLooping;

                for (int l = 0; l < animList[0].frameList.Count; l++)
                {
                    frameListBox.Items.Add(animList[0].frameList[l]);

                }

                durationNumericUpDown.Value = (decimal)animList[0].frameList[0].FDuration;

                anchorXNumericUpDown.Value = animList[0].frameList[0].FanchorX;
                anchorYNumericUpDown.Value = animList[0].frameList[0].FanchorY;

                sourceXNumericUpDown.Value = animList[0].frameList[0].sourceRect.HitBoxX;
                sourceYNumericUpDown.Value = animList[0].frameList[0].sourceRect.HitBoxY;
                sourceHNumericUpDown.Value = animList[0].frameList[0].sourceRect.HitBoxLength;
                sourceWNumericUpDown.Value = animList[0].frameList[0].sourceRect.HitBoxWidth;

                for (int a = 0; a < animList[0].frameList[0].activeHitBoxList.Count; a++)
                    activeListBox.Items.Add(animList[0].frameList[0].activeHitBoxList[a]);

                activeXNumericUpDown.Value = animList[0].frameList[0].activeHitBoxList[0].HitBoxX;
                activeYNumericUpDown.Value = animList[0].frameList[0].activeHitBoxList[0].HitBoxY;
                activeWNumericUpDown.Value = animList[0].frameList[0].activeHitBoxList[0].HitBoxWidth;
                activeHNumericUpDown.Value = animList[0].frameList[0].activeHitBoxList[0].HitBoxLength;

                passiveXNumericUpDown.Value = animList[0].frameList[0].passiveHitBox.HitBoxX;
                passiveYNumericUpDown.Value = animList[0].frameList[0].passiveHitBox.HitBoxY;
                passiveWNumericUpDown.Value = animList[0].frameList[0].passiveHitBox.HitBoxWidth;
                passiveHNumericUpDown.Value = animList[0].frameList[0].passiveHitBox.HitBoxLength;

                triggerTextBox.Text = animList[0].frameList[0].Strigger;

                animationListBox.SelectedIndex = 0;
                frameListBox.SelectedIndex = 0;
                activeListBox.SelectedIndex = 0;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }

        private void openSpriteSheetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "PNG Files(*.png)|*.png";
                dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Images");
                if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\Images"))
                {
                    Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\Images");
                    dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\Images");

                }
                if (DialogResult.OK == dlg.ShowDialog())
                {      
                    currSpriteIndex = TM.LoadTexture(dlg.FileName);// can add scale
                    Bitmap tempbmp = new Bitmap(dlg.FileName);
                    panel1.AutoScrollMinSize = tempbmp.Size;
                    anchorXNumericUpDown.Maximum = tempbmp.Size.Width;
                    anchorYNumericUpDown.Maximum = tempbmp.Size.Height;
                    sourceXNumericUpDown.Maximum = tempbmp.Size.Width;
                    sourceYNumericUpDown.Maximum = tempbmp.Size.Height;
                    sourceWNumericUpDown.Maximum = tempbmp.Size.Width;
                    sourceHNumericUpDown.Maximum = tempbmp.Size.Height;
                    activeXNumericUpDown.Maximum = tempbmp.Size.Width;
                    activeYNumericUpDown.Maximum = tempbmp.Size.Height;
                    activeWNumericUpDown.Maximum = tempbmp.Size.Width;
                    activeHNumericUpDown.Maximum = tempbmp.Size.Height;
                    passiveXNumericUpDown.Maximum = tempbmp.Size.Width;
                    passiveYNumericUpDown.Maximum = tempbmp.Size.Height;
                    passiveWNumericUpDown.Maximum = tempbmp.Size.Width;
                    passiveHNumericUpDown.Maximum = tempbmp.Size.Height;
                }
        }


        private void activeListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int temp1 = animationListBox.SelectedIndex;
            int temp2 = frameListBox.SelectedIndex;
            int temp3 = activeListBox.SelectedIndex;

                activeListBox.Refresh();
                if (temp3 >= 0 && temp2 >= 0)
                {
                    if (animList[temp1].frameList[temp2].activeHitBoxList.Count != 0)
                    {
                        activeXNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[temp3].HitBoxX;
                        activeYNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[temp3].HitBoxY;
                        activeWNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[temp3].HitBoxWidth;
                        activeHNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[temp3].HitBoxLength;
                    }
                    else
                    {
                        activeXNumericUpDown.Value = 0;
                        activeYNumericUpDown.Value = 0;
                        activeWNumericUpDown.Value = 0;
                        activeHNumericUpDown.Value = 0;
                    }
                }
                else { }

        }

        private void frameListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            currFrame = frameListBox.SelectedIndex;
            int temp1 = animationListBox.SelectedIndex;
            int temp2 = frameListBox.SelectedIndex;

            if (temp2 >= 0)
            {
                durationNumericUpDown.Value = (decimal)animList[temp1].frameList[temp2].FDuration;

                anchorXNumericUpDown.Value = animList[temp1].frameList[temp2].FanchorX;
                anchorYNumericUpDown.Value = animList[temp1].frameList[temp2].FanchorY;

                sourceXNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxX;
                sourceYNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxY;
                sourceHNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxLength;
                sourceWNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxWidth;

                activeListBox.Items.Clear();
                for (int a = 0; a < animList[temp1].frameList[temp2].activeHitBoxList.Count; a++)
                    activeListBox.Items.Add(animList[temp1].frameList[temp2].activeHitBoxList[a]);

                if (animList[temp1].frameList[temp2].activeHitBoxList.Count != 0)
                {
                    activeXNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxX;
                    activeYNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxY;
                    activeWNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxWidth;
                    activeHNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxLength;
                }
                else
                {
                    activeXNumericUpDown.Value = 0;
                    activeYNumericUpDown.Value = 0;
                    activeWNumericUpDown.Value = 0;
                    activeHNumericUpDown.Value = 0;
                }

                passiveXNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxX;
                passiveYNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxY;
                passiveWNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxWidth;
                passiveHNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxLength;

                triggerTextBox.Text = animList[temp1].frameList[temp2].Strigger;
            }
            else { }

        }

        private void animationListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int temp1 = animationListBox.SelectedIndex;

            if (temp1 > -1)
            {
                animNameTextBox.Text = animList[temp1].AnimName;
                imageNameTextBox.Text = animList[temp1].ImageName;
                redNumericUpDown.Value = animList[temp1].BackColorR;
                greenNumericUpDown.Value = animList[temp1].BackColorG;
                blueNumericUpDown.Value = animList[temp1].BackColorB;
                loopingCheckBox.Checked = animList[temp1].IsLooping;

                frameListBox.Items.Clear();
                
                for (int l = 0; l < animList[temp1].frameList.Count; l++)
                {
                    frameListBox.Items.Add(animList[temp1].frameList[l]);
                }

                if (animList[temp1].frameList.Count > 0)
                {
                    durationNumericUpDown.Value = (decimal)animList[temp1].frameList[0].FDuration;

                    anchorXNumericUpDown.Value = animList[temp1].frameList[0].FanchorX;
                    anchorYNumericUpDown.Value = animList[temp1].frameList[0].FanchorY;

                    sourceXNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxX;
                    sourceYNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxY;
                    sourceHNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxLength;
                    sourceWNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxWidth;

                    activeListBox.Items.Clear();
                    for (int a = 0; a < animList[temp1].frameList[0].activeHitBoxList.Count; a++)
                        activeListBox.Items.Add(animList[temp1].frameList[0].activeHitBoxList[a]);

                    if (animList[temp1].frameList[0].activeHitBoxList.Count != 0)
                    {
                        activeXNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxX;
                        activeYNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxY;
                        activeWNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxWidth;
                        activeHNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxLength;
                    }
                    else
                    {
                        activeXNumericUpDown.Value = 0;
                        activeYNumericUpDown.Value = 0;
                        activeWNumericUpDown.Value = 0;
                        activeHNumericUpDown.Value = 0;
                    }


                    passiveXNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxX;
                    passiveYNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxY;
                    passiveWNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxWidth;
                    passiveHNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxLength;

                    triggerTextBox.Text = animList[temp1].frameList[0].Strigger;
                }
            }
            else { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CAnimation newAnim = new CAnimation();

            newAnim.AnimName = "New Animation";

            CFrame newFrame = new CFrame();

            newFrame.Name1 = "frame0";
            newFrame.passiveHitBox.HitBoxX = 0;
            newFrame.passiveHitBox.HitBoxY = 0;
            newFrame.passiveHitBox.HitBoxWidth = 0;
            newFrame.passiveHitBox.HitBoxLength = 0;
            newFrame.passiveHitBox.ActiveName = "passiveHitbox";

            newFrame.sourceRect.HitBoxX = 0;
            newFrame.sourceRect.HitBoxY = 0;
            newFrame.sourceRect.HitBoxWidth = 0;
            newFrame.sourceRect.HitBoxLength = 0;
            newFrame.sourceRect.ActiveName = "sourceRect";

            CFrame.Rect tempRect = new CFrame.Rect();
            tempRect.HitBoxX = 0;
            tempRect.HitBoxY = 0;
            tempRect.HitBoxWidth = 0;
            tempRect.HitBoxLength = 0;
            tempRect.ActiveName = "activeRect0";
            newFrame.activeHitBoxList.Add(tempRect);

            newAnim.frameList.Add(newFrame);

            animList.Add(newAnim);


            int temp1 = animationListBox.SelectedIndex;
            if (temp1 == -1)
                temp1 = 0;

            animNameTextBox.Text = animList[temp1].AnimName;
            imageNameTextBox.Text = animList[temp1].ImageName;
            redNumericUpDown.Value = animList[temp1].BackColorR;
            greenNumericUpDown.Value = animList[temp1].BackColorG;
            blueNumericUpDown.Value = animList[temp1].BackColorB;
            loopingCheckBox.Checked = animList[temp1].IsLooping;

            frameListBox.Items.Clear();
            for (int l = 0; l < animList[temp1].frameList.Count; l++)
            {
                frameListBox.Items.Add(animList[temp1].frameList[l]);
            }

            durationNumericUpDown.Value = (decimal)animList[0].frameList[0].FDuration;

            anchorXNumericUpDown.Value = animList[temp1].frameList[0].FanchorX;
            anchorYNumericUpDown.Value = animList[temp1].frameList[0].FanchorY;

            sourceXNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxX;
            sourceYNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxY;
            sourceHNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxLength;
            sourceWNumericUpDown.Value = animList[temp1].frameList[0].sourceRect.HitBoxWidth;

            activeListBox.Items.Clear();
            for (int a = 0; a < animList[temp1].frameList[0].activeHitBoxList.Count; a++)
                activeListBox.Items.Add(animList[temp1].frameList[0].activeHitBoxList[a]);

            if (animList[temp1].frameList[0].activeHitBoxList.Count != 0)
            {
                activeXNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxX;
                activeYNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxY;
                activeWNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxWidth;
                activeHNumericUpDown.Value = animList[temp1].frameList[0].activeHitBoxList[0].HitBoxLength;
            }
            else
            {
                activeXNumericUpDown.Value = 0;
                activeYNumericUpDown.Value = 0;
                activeWNumericUpDown.Value = 0;
                activeHNumericUpDown.Value = 0;
            }


            passiveXNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxX;
            passiveYNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxY;
            passiveWNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxWidth;
            passiveHNumericUpDown.Value = animList[temp1].frameList[0].passiveHitBox.HitBoxLength;

            triggerTextBox.Text = animList[temp1].frameList[0].Strigger;
            
            animationListBox.Items.Add(animList[animList.Count -1]);
                animationListBox.SelectedIndex = animationListBox.Items.Count -1;
                animList[animationListBox.SelectedIndex].NumFrames += 1;

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (animList.Count != 0 && animationListBox.SelectedIndex > -1)
            {

                animList.Remove(animList[animationListBox.SelectedIndex]);

                animationListBox.Items.Clear();

                if (animList.Count != 0)
                {
                    for (int l = 0; l < animList.Count; l++)
                    {
                        animationListBox.Items.Add(animList[l]);
                    }
                    animationListBox.SelectedIndex = -1;
                }
                else { }


                frameListBox.Items.Clear();
                int temp2 = 0;
                if (animList.Count != 0)
                {
                    for (int l = 0; l < animList[temp2].frameList.Count; l++)
                    {
                        frameListBox.Items.Add(animList[0].frameList[l]);
                    }

                    frameListBox.SelectedIndex = -1;
                }
                else { }

                activeListBox.Items.Clear();
                if (animList.Count != 0)
                {
                    for (int a = 0; a < animList[0].frameList[0].activeHitBoxList.Count; a++)
                        activeListBox.Items.Add(animList[0].frameList[0].activeHitBoxList[a]);

                    activeListBox.SelectedIndex = -1;
                }


            }
            else { }
        }

        private void animNameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].AnimName = animNameTextBox.Text;

                int temp = animationListBox.SelectedIndex;
                animationListBox.Items.Clear();

                if (animList.Count != 0)
                {
                    for (int l = 0; l < animList.Count; l++)
                    {
                        animationListBox.Items.Add(animList[l]);
                    }
                    animationListBox.SelectedIndex = temp;
                }
                else { }
            }
            else { }

        }

        private void imageNameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].ImageName = imageNameTextBox.Text;
            }
            else { }

        }

        private void redNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].BackColorR = (int)redNumericUpDown.Value;
            }
            else { }
        }

        private void greenNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].BackColorG = (int)greenNumericUpDown.Value;
            }
            else { }
        }

        private void blueNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].BackColorB = (int)blueNumericUpDown.Value;
            }
            else { }
        }

        private void durationNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].FDuration = (float)durationNumericUpDown.Value;
            }
            else { }
        }

        private void anchorXNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].FanchorX = (int)anchorXNumericUpDown.Value;
            }
            else { }
        }

        private void anchorYNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].FanchorY = (int)anchorYNumericUpDown.Value;
            }
            else { }
        }

        private void sourceXNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.HitBoxX= (int)sourceXNumericUpDown.Value;
            }
            else { }
        }

        private void sourceYNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.HitBoxY = (int)sourceYNumericUpDown.Value;
            }
            else { }
        }

        private void sourceHNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.HitBoxLength = (int)sourceHNumericUpDown.Value;
            }
            else { }
        }

        private void sourceWNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.HitBoxWidth = (int)sourceWNumericUpDown.Value;
            }
            else { }
        }

        private void activeXNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            CFrame.Rect temp = new CFrame.Rect();
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1 && activeListBox.SelectedIndex != -1)
            {
               temp = animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[activeListBox.SelectedIndex];
                temp.hitBoxX = (int)activeXNumericUpDown.Value;
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex]
                    .activeHitBoxList[activeListBox.SelectedIndex] = temp;
            }
            else { }
        }

        private void activeYNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            CFrame.Rect temp = new CFrame.Rect();
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1 && activeListBox.SelectedIndex != -1)
            {
                temp = animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[activeListBox.SelectedIndex];
                temp.hitBoxY = (int)activeYNumericUpDown.Value;
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex]
                    .activeHitBoxList[activeListBox.SelectedIndex] = temp;
            }
            else { }
        }

        private void activeHNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            CFrame.Rect temp = new CFrame.Rect();
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1 && activeListBox.SelectedIndex != -1)
            {
                temp = animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[activeListBox.SelectedIndex];
                temp.hitBoxLength = (int)activeHNumericUpDown.Value;
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex]
                    .activeHitBoxList[activeListBox.SelectedIndex] = temp;
            }
            else { }
        }

        private void activeWNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            CFrame.Rect temp = new CFrame.Rect();
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1 && activeListBox.SelectedIndex != -1)
            {
                temp = animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[activeListBox.SelectedIndex];
                temp.hitBoxWidth = (int)activeWNumericUpDown.Value;
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex]
                    .activeHitBoxList[activeListBox.SelectedIndex] = temp;
            }
            else { }
        }

        private void passiveXNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
           
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].passiveHitBox.hitBoxX = (int)passiveXNumericUpDown.Value;
            }
            else { }
        }

        private void passiveYNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].passiveHitBox.hitBoxY = (int)passiveYNumericUpDown.Value;
            }
            else { }
        }

        private void passiveHNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].passiveHitBox.hitBoxLength = (int)passiveHNumericUpDown.Value;
            }
            else { }
        }

        private void passiveWNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].passiveHitBox.hitBoxWidth = (int)passiveWNumericUpDown.Value;
            }
            else { }
        }

        private void triggerTextBox_TextChanged(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].Strigger = triggerTextBox.Text;
            }
            else { }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int temp1 = animationListBox.SelectedIndex;
            if (temp1 != -1)
            {

                CFrame newFrame = new CFrame();

                newFrame.Name1 = "frame" + frameListBox.Items.Count;
                newFrame.passiveHitBox.HitBoxX = 0;
                newFrame.passiveHitBox.HitBoxY = 0;
                newFrame.passiveHitBox.HitBoxWidth = 0;
                newFrame.passiveHitBox.HitBoxLength = 0;
                newFrame.passiveHitBox.ActiveName = "passiveHitbox";

                newFrame.sourceRect.HitBoxX = 0;
                newFrame.sourceRect.HitBoxY = 0;
                newFrame.sourceRect.HitBoxWidth = 0;
                newFrame.sourceRect.HitBoxLength = 0;
                newFrame.sourceRect.ActiveName = "sourceRect";

                CFrame.Rect tempRect = new CFrame.Rect();
                tempRect.HitBoxX = 0;
                tempRect.HitBoxY = 0;
                tempRect.HitBoxWidth = 0;
                tempRect.HitBoxLength = 0;
                tempRect.ActiveName = "activeRect0";
                newFrame.activeHitBoxList.Add(tempRect);

                animList[animationListBox.SelectedIndex].frameList.Add(newFrame);
                animList[animationListBox.SelectedIndex].NumFrames += 1;


                frameListBox.Items.Clear();
                for (int l = 0; l < animList[temp1].frameList.Count; l++)
                {
                    frameListBox.Items.Add(animList[temp1].frameList[l]);
                }

                frameListBox.SelectedIndex = frameListBox.Items.Count -1;
                int temp2 = frameListBox.SelectedIndex;

                durationNumericUpDown.Value = (decimal)animList[temp1].frameList[temp2].FDuration;

                anchorXNumericUpDown.Value = animList[temp1].frameList[temp2].FanchorX;
                anchorYNumericUpDown.Value = animList[temp1].frameList[temp2].FanchorY;

                sourceXNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxX;
                sourceYNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxY;
                sourceHNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxLength;
                sourceWNumericUpDown.Value = animList[temp1].frameList[temp2].sourceRect.HitBoxWidth;

                activeListBox.Items.Clear();
                for (int a = 0; a < animList[temp1].frameList[temp2].activeHitBoxList.Count; a++)
                    activeListBox.Items.Add(animList[temp1].frameList[temp2].activeHitBoxList[a]);

                if (animList[temp1].frameList[temp2].activeHitBoxList.Count != 0)
                {
                    activeXNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxX;
                    activeYNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxY;
                    activeWNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxWidth;
                    activeHNumericUpDown.Value = animList[temp1].frameList[temp2].activeHitBoxList[0].HitBoxLength;
                }
                else
                {
                    activeXNumericUpDown.Value = 0;
                    activeYNumericUpDown.Value = 0;
                    activeWNumericUpDown.Value = 0;
                    activeHNumericUpDown.Value = 0;
                }


                passiveXNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxX;
                passiveYNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxY;
                passiveWNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxWidth;
                passiveHNumericUpDown.Value = animList[temp1].frameList[temp2].passiveHitBox.HitBoxLength;

                triggerTextBox.Text = animList[temp1].frameList[temp2].Strigger;

            }
            else { }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex > -1 && animList[animationListBox.SelectedIndex].frameList.Count != 0 && frameListBox.SelectedIndex > -1)
            {

                animList[animationListBox.SelectedIndex].frameList.Remove(animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex]);
   

                frameListBox.Items.Clear();
                
                if (animList[animationListBox.SelectedIndex].frameList.Count != 0)
                {
                    for (int l = 0; l < animList[animationListBox.SelectedIndex].frameList.Count; l++)
                    {
                        animList[animationListBox.SelectedIndex].frameList[l].Name1 = "frame" + l;
                        frameListBox.Items.Add(animList[animationListBox.SelectedIndex].frameList[l]);
                    }

                    frameListBox.SelectedIndex = -1;
                }
                else { }

                activeListBox.Items.Clear();
               


            }
            else { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1 && frameListBox.SelectedIndex != -1)
            {
                CFrame.Rect tempRect = new CFrame.Rect();
                tempRect.HitBoxX = 0;
                tempRect.HitBoxY = 0;
                tempRect.HitBoxWidth = 0;
                tempRect.HitBoxLength = 0;
                tempRect.ActiveName = "activeRect" + animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.Count;


                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.Add(tempRect);

                activeListBox.Items.Clear();
                for (int a = 0; a < animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.Count; a++)
                    activeListBox.Items.Add(animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[a]);
            }
            else { }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (activeListBox.SelectedIndex > -1 && animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.Count != 0 && frameListBox.SelectedIndex > -1)
            {

                animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.
                    Remove(animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[activeListBox.SelectedIndex]);

                activeListBox.Items.Clear();

                if (animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.Count != 0)
                {
                    for (int l = 0; l < animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList.Count; l++)
                    {
                        animList[animationListBox.SelectedIndex].frameList[l].Name1 = "active" + l;
                        activeListBox.Items.Add(animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].activeHitBoxList[l]);
                    }

                    activeListBox.SelectedIndex = -1;
                }

                else { }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex > -1)
            {

                ColorDialog cpd = new ColorDialog();
                
                if (cpd.ShowDialog() == DialogResult.OK)
                {
                    Color colour;
                    colour = cpd.Color;
                    animList[animationListBox.SelectedIndex].BackColorR = colour.R;
                    animList[animationListBox.SelectedIndex].BackColorG = colour.G;
                    animList[animationListBox.SelectedIndex].BackColorB = colour.B;

                    redNumericUpDown.Value = colour.R;
                    greenNumericUpDown.Value = colour.G;
                    blueNumericUpDown.Value = colour.B;
                }
            }
            else { }
            
        }


        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            if (Directory.Exists("\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites"))
                Directory.SetCurrentDirectory("\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites");

            
            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\Sprites"))
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\Sprites");

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Sprites"))
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Sprites");

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Sprites"))
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Sprites");

            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites"))
            {
                Directory.SetCurrentDirectory(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites");
                sfd.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\..\\..\\..\\NationalBombBallLeague\\NationalBombBallLeague\\Resources\\Sprites");
            }


            sfd.InitialDirectory = Directory.GetCurrentDirectory();

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter write = XmlWriter.Create(sfd.FileName, settings);

                write.WriteStartDocument();
                write.WriteStartElement("animations");
                write.WriteAttributeString("num", animList.Count.ToString());


                for (int a = 0; a < animList.Count; a++)
                {
                    write.WriteStartElement("anim");

                    write.WriteStartElement("animName");
                    write.WriteString(animList[a].AnimName.ToString());
                    write.WriteEndElement();

                    write.WriteStartElement("animImage");
                    write.WriteString(animList[a].ImageName.ToString());
                    write.WriteEndElement();

                    write.WriteStartElement("testInfo");
                    write.WriteAttributeString("numFrames", animList[a].frameList.Count.ToString());
                    write.WriteAttributeString("backColorR", animList[a].BackColorR.ToString());
                    write.WriteAttributeString("backColorG", animList[a].BackColorG.ToString());
                    write.WriteAttributeString("backColorB", animList[a].BackColorB.ToString());

                    int playing;
                    if (animList[a].IsPlaying == true)
                        playing = 1;
                    else
                        playing = 0;

                    int looping;
                    if (animList[a].IsLooping == true)
                        looping = 1;
                    else
                        looping = 0;


                    write.WriteAttributeString("playing", playing.ToString());
                    write.WriteAttributeString("looping", looping.ToString());

                    write.WriteEndElement();

                    for (int f = 0; f < animList[a].frameList.Count; f++)
                    {
                        
                        write.WriteStartElement("frame");
                        write.WriteAttributeString("duration", animList[a].frameList[f].FDuration.ToString());
                        write.WriteAttributeString("sourceRectL", animList[a].frameList[f].sourceRect.hitBoxX.ToString());
                        write.WriteAttributeString("sourceRectT", animList[a].frameList[f].sourceRect.hitBoxY.ToString());

                      int temp2 = animList[a].frameList[f].sourceRect.hitBoxWidth + animList[a].frameList[f].sourceRect.hitBoxX; 
                        write.WriteAttributeString("sourceRectR", temp2.ToString());
                       temp2 = animList[a].frameList[f].sourceRect.hitBoxLength + animList[a].frameList[f].sourceRect.hitBoxY; 
                        write.WriteAttributeString("sourceRectB", temp2.ToString());

                        write.WriteAttributeString("anchorX", animList[a].frameList[f].FanchorX.ToString());
                        write.WriteAttributeString("anchorY", animList[a].frameList[f].FanchorY.ToString());
                        write.WriteAttributeString("trigger", animList[a].frameList[f].Strigger.ToString());
                        write.WriteAttributeString("numAR", animList[a].frameList[f].activeHitBoxList.Count.ToString());
                        write.WriteEndElement();

                        for (int c = 0; c < animList[a].frameList[f].activeHitBoxList.Count; c++)
                        {
                            write.WriteStartElement("active");
                            write.WriteAttributeString("activeL", animList[a].frameList[f].activeHitBoxList[c].hitBoxX.ToString());
                            write.WriteAttributeString("activeT", animList[a].frameList[f].activeHitBoxList[c].hitBoxY.ToString());

                            int temp = animList[a].frameList[f].activeHitBoxList[c].hitBoxWidth + animList[a].frameList[f].activeHitBoxList[c].hitBoxX;
                            write.WriteAttributeString("activeR", temp.ToString());
                            temp = animList[a].frameList[f].activeHitBoxList[c].hitBoxLength + animList[a].frameList[f].activeHitBoxList[c].hitBoxY;
                            write.WriteAttributeString("activeB", temp.ToString());

                            write.WriteEndElement();
                        }

                        write.WriteStartElement("passive");
                        write.WriteAttributeString("passiveL", animList[a].frameList[f].passiveHitBox.hitBoxX.ToString());
                        write.WriteAttributeString("passiveT", animList[a].frameList[f].passiveHitBox.hitBoxY.ToString());

                        int temp3 = animList[a].frameList[f].passiveHitBox.hitBoxWidth + animList[a].frameList[f].passiveHitBox.hitBoxX;
                        write.WriteAttributeString("passiveR", animList[a].frameList[f].passiveHitBox.hitBoxWidth.ToString());
                        temp3 = animList[a].frameList[f].passiveHitBox.hitBoxLength + animList[a].frameList[f].passiveHitBox.hitBoxY;
                        write.WriteAttributeString("passiveB", animList[a].frameList[f].passiveHitBox.hitBoxLength.ToString());

                        
                        write.WriteEndElement();
                    }

                    write.WriteEndElement();
                   


                }
                    
                    write.WriteEndDocument();
                    write.Flush();
                    write.Close();
            }
        }

        private void panel1_Scroll(object sender, ScrollEventArgs e)
        {
            Render();
        }

      

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            offset.X = origin.X + panel1.AutoScrollPosition.X;
            offset.Y = origin.Y + panel1.AutoScrollPosition.Y;

            if (anchorControlRadioButton.Checked == true)
            {
                if (animationListBox.SelectedIndex > -1 && frameListBox.SelectedIndex > -1)
                {
                    animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].FanchorX = e.Location.X + offset.X;
                    animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].FanchorY = e.Location.Y + offset.Y;
                }
            }

            if (renderControlRadioButton.Checked == true)
            {
                if ( animationListBox.SelectedIndex > -1 && frameListBox.SelectedIndex > -1)
                {
                    animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxX = e.Location.X + offset.X;
                    animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxY = e.Location.Y + offset.Y;
                }
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            offset.X = origin.X + panel1.AutoScrollPosition.X;
            offset.Y = origin.Y + panel1.AutoScrollPosition.Y;

            if (anchorControlRadioButton.Checked == true)
            {
            }

            if (renderControlRadioButton.Checked == true)
            {
                if (animationListBox.SelectedIndex > -1 && frameListBox.SelectedIndex > -1)
                {
                    animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxWidth = e.Location.X + offset.X - animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxX;
                    animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxLength = e.Location.Y + offset.Y - animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxY;

                    if (animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxWidth + animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxX < animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxX)
                   {
                       int temp1 = animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxWidth;
                       int temp2 = animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxX;
                       animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxX += temp1;
                       animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxWidth = -temp2;

                   }
                   if (animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxLength < animList[animationListBox.SelectedIndex].frameList[frameListBox.SelectedIndex].sourceRect.hitBoxY)
                   {
                       
                   }
                }
            }
        }

        private void playOnceButton_Click(object sender, EventArgs e)
        {
            if (animationListBox.SelectedIndex != -1)
            {
                playOnce = true;
                currFrame = 0;
                pause = true;
                loopCheckBox.Checked = false;
            }
        }

        private void loadFrameDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (animList.Count > 0)
            {
                for (int i = 0; i < animList.Count; i++)
                {
                    animList.Remove(animList[i]);
                    i--;
                }
                animationListBox.Items.Clear();

                frameListBox.Items.Clear();

                activeListBox.Items.Clear();

                animNameTextBox.Text = "";
                imageNameTextBox.Text = "";
                redNumericUpDown.Value = 0;
                greenNumericUpDown.Value = 0;
                blueNumericUpDown.Value = 0;
                loopingCheckBox.Checked = false;

                frameListBox.Items.Clear();

                durationNumericUpDown.Value = 0.0M;

                anchorXNumericUpDown.Value = 0;
                anchorYNumericUpDown.Value = 0;

                sourceXNumericUpDown.Value = 0;
                sourceYNumericUpDown.Value = 0;
                sourceHNumericUpDown.Value = 0;
                sourceWNumericUpDown.Value = 0;
                activeXNumericUpDown.Value = 0;
                activeYNumericUpDown.Value = 0;
                activeWNumericUpDown.Value = 0;
                activeHNumericUpDown.Value = 0;



                passiveXNumericUpDown.Value = 0;
                passiveYNumericUpDown.Value = 0;
                passiveWNumericUpDown.Value = 0;
                passiveHNumericUpDown.Value = 0;

                triggerTextBox.Text = "";
            }
            if (currSpriteIndex != -1)
            {
                TM.UnloadTexture(currSpriteIndex);
                currSpriteIndex = -1;
            }
        }

        

        


        










        
    }
}
